<?php

class Workset_Controller_Action_Helper_ActionLayout extends Zend_Controller_Action_Helper_Abstract {

    const CSS_STYLE =   'cssStyle';
    const CSS_FILE =    'cssLink';
    const JS_CODE =     'script';
    const JS_FILE =     'scriptFile';
    const PAGE_TITLE =  'pageTitle';

    function direct($settings) {
        //dump($settings);
        foreach ($settings as $key => $spec) {
            switch ($key) {
                case self::CSS_STYLE :
                    $this->addStyle($spec);
                break;

                case self::CSS_FILE :
                    $this->addLink($spec);
                break;

                case self::JS_CODE :
                    $this->addScript($spec);
                break;

                case self::JS_FILE :
                    $this->addScriptFile($spec);
                break;

                case self::PAGE_TITLE :
                    $this->setPageTitle($spec);
                break;
            }
        }

    }

    /**
     * @param  $styles
     * @return Application_Action_Helper_ActionLayout
     *
     * Добавляет css-стили страницы (тег <style>)
     */
    function addStyle($styles) {
        $view = $this->getActionController()->initView();
        foreach ((array)$styles as $key => $style) {
            $attrs = !is_int($key) ? array('media' => $key) : null;
            $view->headStyle()->appendStyle($style, $attrs);
        }
        return $this;
    }

    /**
     * @param  $cssLink
     * @return Application_Action_Helper_ActionLayout
     *
     * Добавляет css-файл (тег <link>)
     */
    function addLink($cssLink) {
        $view = $this->getActionController()->initView();
        foreach ((array)$cssLink as $key => $link) {
            $view->headLink()->offsetSetStylesheet($key, $link);
        }
        return $this;
    }

    function addScript($scripts) {
        $view = $this->getActionController()->initView();

    }

    function addScriptFile($scripts) {
        $view = $this->getActionController()->initView();
        foreach ((array)$scripts as $key => $script) {
            $view->headScript()->offsetSetFile($key, $script);
        }
    }

    function setPageTitle($title) {
        Zend_Layout::getMvcInstance()->assign('title', $title);
    }

}

?>